package org.deng.learn.webflux.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.deng.learn.webflux.dao.SysUserRepository;
import org.deng.learn.webflux.dao.UserRepository;
import org.deng.learn.webflux.entity.SysUser;
import org.deng.learn.webflux.entity.User;
import org.deng.learn.webflux.util.ValidUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.validation.Valid;

/**
 * @author 邓力宾
 * @date 2020/12/18 15:26
 * @desc
 */
@RestController
@RequestMapping("/sys_user")
@Api(tags = {"2、用户接口(mariadb)"})

public class SysUserController {
    private SysUserRepository userRepository;

    public SysUserController(SysUserRepository userRepository){
        this.userRepository = userRepository;
    }

    /**
     * 以数组形式一次返回数据
     * @return
     */
    @GetMapping("/")
    @ApiOperation(value = "以数组形式一次返回数据")
    public Flux<SysUser> getAll(){
        return userRepository.findAll();
    }

    /**
     * 以sse形式多次返回数据
     * @return
     */
    @ApiOperation(value = "多次返回数据")
    @GetMapping(value = "/stream/all", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<SysUser> getStreamAll(){
        return userRepository.findAll();
    }

    /**
     * 保存用户
     * @return
     */
    @ApiOperation(value = "保存用户")
    @PostMapping(value = "/save")
    public Mono<SysUser> save(@Valid @RequestBody SysUser user){ //增加参数校验 条件查看User类的字段注解
        return userRepository.save(user);
    }
    /**
     * 删除用户 rest规范 成功返回200 不存在返回404
     * @return
     */
    @ApiOperation(value = "删除用户")
    @DeleteMapping(value = "/{id}")
    public Mono<ResponseEntity<Void>> delete(@PathVariable Long id){

        return userRepository.findById(id)
                //当要操作数据，并返回一个Mono时，使用flatMap
                //如果不操作数据，只是转换数据使用map
            .flatMap(user->
                    this.userRepository.delete(user)
                        .then(Mono.just(new ResponseEntity<Void>(HttpStatus.OK))))
            .defaultIfEmpty(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * 修改 存在返回200， 不存在返回404
     * @param id
     * @return
     */
    @ApiOperation(value = "修改用户")
    @PutMapping("/{id}")
    public Mono<ResponseEntity<SysUser>> updateUser(@PathVariable Long id,
                                                 @Valid @RequestBody SysUser user){
        return this.userRepository
                .findById(id)
                //flatMap操作数据
                .flatMap(u->{
                    u.setFullName(user.getFullName());
                    u.setUsername(user.getUsername());
                    return this.userRepository.save(u);
                    //map 转换数据
                }).map(u->new ResponseEntity<>(u, HttpStatus.OK))
                  .defaultIfEmpty(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * 根据id查询 存在返回200， 不存在返回404
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping("/{id}")
    public Mono<ResponseEntity<SysUser>> get(@PathVariable Long id){
        return this.userRepository.findById(id)
                .map(u -> new ResponseEntity<>(u, HttpStatus.OK)
                ).defaultIfEmpty(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }
}
